const koa = require("koa")
const app = new koa()
const router = require("koa-router")()
const cheerio = require("cheerio")
const puppeteer = require('puppeteer')
router.get("/detail/:id",async ctx=>{
    let {id} = ctx.request.params
    const browser = await puppeteer.launch({
        defaultViewport:null
    });
    const page = await browser.newPage()
    await page.goto(`https://movie.douban.com/subject/${id}/`);
    var content = await page.content()
    var $ = cheerio.load(content,{
        decodeEntities:false
    })
    var result = {}
    var pic = $("#mainpic").find(".nbgnbg img").attr("src")
    var title = $("#content>h1>span:first-child").html().replace(/[A-z]/g,"")
    var EngTitle = $("#mainpic").find(".nbgnbg img").attr("alt")
    var year = $("#content>h1").find(".year").html()
    var rank = $(".top250").find(".top250-no").html()
    var blank = $(".top250").find(".top250-link a").html()
    var nation=[]
    var country = $("#info span[property='v:initialReleaseDate']")
    country.each((index,item)=>{
        var str = $(item).html()
        nation.push(str)
    })
    var place = nation[1].split('(')[1].split(')')[0]
    var arr = []
    arr.push(place)
    var type = $("#info span[property='v:genre']")
    type.each((index,item)=>{
        var res = $(item).html()
        arr.push(res)
    })
    var time = $("#info>span[property='v:runtime']").html()
    arr.push(
        nation[0],
        time
    )
    var raiting = $(".rating_self").find(".rating_num").html()
    var comment = $(".rating_sum").find(".rating_people span").html()
    var intro = $(".indent").find("span[property='v:summary']").text()
    var actors =[]
    var res = $(".celebrities-list").find(".celebrity")
    res.each((index,value)=>{
        var obj ={}
        obj.images=$(value).find("a div").css('background-image').replace('url(','').replace(')','')
        obj.name = $(value).find(".info .name a").text()
        obj.role = $(value).find(".info .role").text()
        actors.push(obj)
    })
    result = {pic,title,EngTitle,year,rank,blank,arr,raiting,comment,intro,actors}
    ctx.body={
        code:200,
        msg:"详情页数据",
        data:result
    }
})
    

app.use(router.routes())
app.listen(8080)